
var productId = getQuery("productId");

window.onload = function (){
    if (productId != null&&productId != ''){
        buy();
    }else {
        showShopCart();
    }
}

function showShopCart(){  //显示购物车信息
    axios.get("/customer/shop/findShopCart",null).then(e => {
        var info = e.data;
        var total = 0;
        var str = "";
        for (var i=0;i<info.length;i++){
            var obj = info[i];
            str += `<tr><td>${obj.product.name}</td><td>${obj.product.price}</td><td>${obj.product.createAddress}</td>
                    <td><input type="number" value="${obj.count}" onblur="javascript:update(${obj.id},this)"></td>
                    <td>${obj.product.price*obj.count}</td><td><a href="javascript:del(${obj.id})">移除商品</a></td></tr>`

            total += obj.product.price * obj.count;
        }
        $('shopCartData').innerHTML = str;
        $('totalSpan').innerHTML = total;
    })
}
function buy(){
    axios.get("/customer/shop/buy",{
        params:{
            productId:productId
        }
    }).then(e => {
        if (e.data == 'ok'){
            showShopCart();
        }
    })
}

function del(shopCartId){
    axios.get("/customer/shop/del",{
        params: {
            shopCartId
        }
    }).then(e => {
        if (e.data == 'ok'){
            showShopCart()
        }
    })
}

function update(shopCartId,textObj){
    axios.get("/customer/shop/update",{
        params:{
            shopCartId:shopCartId,
            count:parseInt(textObj)
    }
    }).then(e => {
        if (e.data == 'ok'){
            showShopCart();
        }else {
            alert("购物车数量更新失败")
        }
    })
}

function addOrder(){
    location.href = "/customer/addOrder.html?totalPrice="+$('totalSpan').innerHTML;
}
